﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Navigation;
using Silvermoon.Shapes;
using Silvermoon.Utils;
using Silvermoon.Core;
using System.Drawing;
using Silvermoon.Windows;
using Silvermoon.Images;
using Silvermoon.UI;
using Silvermoon.Windows.Styles;

namespace Silvermoon.Controls
{
    public class Picker : PickerBase
    {
        private static readonly VisualStateManager itemStateManager = new VisualStateManager();
        public static readonly Style PickerItemStyle = new Style { StateManager = ItemStateManagers.PickerItem, Opacity = 164 };

        private Margin lenseMargin = Margin.Zero;
        private Shape lense;

        public Picker()
            : base()
        {
            Styles.SetClass<Item>(PickerItemStyle);
        }


        protected override void Dispose(bool dispose)
        {
            DisposeObject(lense);
            base.Dispose(dispose);
        }

        /// <summary>
        /// Gets or sets the shape for the lense.
        /// </summary>
        public Shape Lense
        {
            get
            {
                if (lense == null) lense = CreateLense();
                return lense;
            }
            set
            {
                if (lense != value)
                {
                    if (lense != null) lense.Dispose();
                    lense = value;
                    Invalidate(InvalidationFlag.Appearance);
                }
            }
        }

        public Rectangle LenseRect
        {
            get
            {
                if (lense == null) return Rectangle.Empty;
                int y = (Height - ItemHeight) / 2;
                Size size = new Size(ClientWidth, ItemHeight);
                Rectangle r = LenseMargin.Layout(lense.Bounds, size);
                r.Y += y;
                return Control.Scale(r);
            }
        }

        public Margin LenseMargin
        {
            get { return lenseMargin ?? Margin.Zero; }
            set
            {
                if (!LenseMargin.Equals(value))
                {
                    lenseMargin = value;
                    Invalidate(InvalidationFlag.Layout);
                }
            }
        }

        /// <summary>
        /// Creates the default shape for the lense.
        /// </summary>
        /// <returns>Shape for lense.</returns>
        protected virtual Shape CreateLense()
        {
            return new LenseShape();
        }

        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Size) && lense != null)
            {
                lense.Bounds = LenseRect;
            }
            base.OnLayout();
        }

        protected override void PreLayout()
        {
            if (Lense != null) lense.PreLoad();
            base.PreLayout();
        }

        protected internal override void OnRender(Renderer renderer)
        {
            if (lense != null)
            {
                int alpha = renderer.Alpha;
                Rectangle scissorBox = renderer.ScissorBox;
                base.OnRender(renderer);
                renderer.ScissorBox = scissorBox;
                renderer.RenderShape(lense, alpha);
            }
            else
            {
                base.OnRender(renderer);
            }
        }
    }
}
